package com.etalk.crm.serviceImpl;

import com.etalk.crm.dao.PracticeQuestionsMapper;
import com.etalk.crm.pojo.PracticeQuestionsRecord;
import com.etalk.crm.pojo.PracticeQuestionsResult;
import com.etalk.crm.service.PracticeQuestionsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Jordan
 */
@Service
public class PracticeQuestionsServiceImpl implements PracticeQuestionsService {

    @Resource
    private PracticeQuestionsMapper practiceQuestionsMapper;

    /**
     * 查询未掌握知识点练习题完成情况列表
     * @param record 查询条件
     * @param pageNum 页码
     * @param pageSize 每页记录数
     * @return
     */
    @Override
    public PageInfo<PracticeQuestionsRecord> searchPracticeQuestionsPage(PracticeQuestionsRecord record, int pageNum, int pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<PracticeQuestionsRecord> list=practiceQuestionsMapper.selectPracticeQuestionsByPage(record);

        return new PageInfo<>(list);
    }

    /**
     * 查询课后练习完成情况
     * @param lessonId 课程id
     * @return
     */
    @Override
    public List<Map<String,Object>> searchPracticeQuestionsResultByLessonId(int lessonId){
        List<Map<String,Object>> resultList=new ArrayList<>();
        List<PracticeQuestionsResult> list=practiceQuestionsMapper.selectPracticeQuestionsResultByLessonId(lessonId);
        if (list!=null && list.size()>0){
            int tempBankId=0;
            Map<String,Object> bankMap=null;
            List<Map<String,String>> listAnswer=null;
            for (PracticeQuestionsResult result:list){
                if (result.getBankId()!=tempBankId){
                    tempBankId=result.getBankId();
                    bankMap=new HashMap<>(6);
                    bankMap.put("qtitle",result.getQtitle());
                    bankMap.put("questionResultImg",result.getQuestionResult().contains("://")?result.getQuestionResult():"");
                    bankMap.put("questionResult",result.getQuestionResult().contains("://")?"":result.getQuestionResult());
                    bankMap.put("isTrue",result.getIsTrue());
                    bankMap.put("audioUrl",result.getAudioUrl());
                    bankMap.put("listBank",null);
                    if (!StringUtils.isEmpty(result.getBankImg())){
                        bankMap.put("listBank",result.getBankImg().split(","));
                    }
                    listAnswer=new ArrayList<>();
                    bankMap.put("listAnswer",listAnswer);
                    resultList.add(bankMap);
                }
                Map<String,String> answerMap=new HashMap<>(2);
                answerMap.put("answer",result.getAnswer());
                answerMap.put("imgUrl",result.getImgUrl());
                listAnswer.add(answerMap);
            }
        }

        return resultList;
    }
}
